<template>
	<view class="u-p-l-50 u-p-r-50 u-d">
		<u-form :model="form" :error-type="errorType" ref="uForm">
			<u-form-item label="姓名" prop="name" required>
				<u-input v-model="form.name" />
			</u-form-item>
			<u-form-item label="密码" prop="password" required>
				<u-input type="password" v-model="form.password" />
			</u-form-item>
		</u-form>
		<u-button class="u-m-t-50" @click="submit">提交</u-button>
	</view>
</template>

<script>
export default {
	data() {
		return {
			errorType: ['message'],
			form: {
				name: '',
				password: '',
			},
			rules: {
				name: [
					{ 
						required: true, 
						message: '请输入新昵称', 
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change','blur'],
					}
				],
				password: [
					{
						min: 6, 
						message: '请输入至少6位新密码', 
						trigger: 'change'
					}
				]
			}
		};
	},
	methods: {
		 submit() {
			this.$refs.uForm.validate(async valid => {
				if (valid) {

					const res =  await this.$u.api.updatauser(this.form)
				    this.$u.utils.updateUser()
					this.$u.toast('更新成功')
						
					setTimeout(()=>{
						this.$u.route({
							type: 'navigateBack',
							delta: 5
						})
							
					},2000)		

				} 
			});
		},

	},
	// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
	onReady() {
		this.$refs.uForm.setRules(this.rules);
		this.form.name = this.vuex_user.name
	}
};
</script>